﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BLL;
using System.Xml;
using System.Drawing.Printing;

namespace Sales
{
    public partial class frmSetPrint : Form
    {
        private static frmSetPrint setPrint = null;
        private BasicBLL basicBLL = null;


        public frmSetPrint()
        {
            InitializeComponent();
            this.dataGridView1.DataSource = ChanageData();
            GetComPrintList();
            ReadXml();
            this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
 

        }
        public static frmSetPrint CreateInstance()
        {
            if (null == setPrint)
            {
                setPrint = new frmSetPrint();
            }
            return setPrint;
        }

        private string GetAppConfig()
        {
            string name = "";
            XmlDocument doc = new XmlDocument();
            string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + @"\config.xml";
            doc.Load(path);    //加载Xml文件  
            XmlElement rootElem = doc.DocumentElement;   //获取根节点  
            XmlNodeList personNodes = rootElem.GetElementsByTagName("Info"); //获取person子节点集合  
            foreach (XmlNode node in personNodes)
            {
                XmlNodeList subAgeNodes = ((XmlElement)node).GetElementsByTagName("Storename");  //获取age子XmlElement集合  
                if (subAgeNodes.Count > 0)
                {
                    name = subAgeNodes[0].InnerText;
                }
            }
            return name;
        }
        private void GetComPrintList()
        {

            //查找系统安装的打印机
            foreach (String pkInstalledPrinters in PrinterSettings.InstalledPrinters)
            {
                this.comPrint.Items.Add(pkInstalledPrinters);
            }
           // this.comPrint.SelectedIndex = 0;
        }


        private void frmSetPrint_Load(object sender, EventArgs e)
        {

        }

        private void gbtnAdd_Click(object sender, EventArgs e)
        {
            string printName = this.comPrint.SelectedItem.ToString();
            string strNumber = this.cTextBox1.Text;
            int num = 0;
           
            basicBLL = new BasicBLL();
            if (printName != "" && strNumber != "")
            {
                num = int.Parse(strNumber);
                basicBLL = new BasicBLL();
                int type = 1;
                if (this.radio2.Checked)
                {
                    type = 2;
                }

                SetPrinters(printName, num, type);

                this.dataGridView1.DataSource = ChanageData();
            }
            else
            {
                Message.MsgBoxWarn("请先选择打印机");
            }
        }
        private DataTable ChanageData()
        {

            XmlDocument doc = new XmlDocument();
            string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + @"\config.xml";
            doc.Load(path);    //加载Xml文件  
            XmlElement rootElem = doc.DocumentElement;   //获取根节点  

            XmlNodeList personNodes = rootElem.GetElementsByTagName("PrintInfo"); //获取PrintInfo 子节点集合  

            string printName = "";
            string number = "";
            string type = "";


            DataTable dt = new DataTable();

            dt.Columns.Add("名称");
            dt.Columns.Add("次数");
            dt.Columns.Add("类型");

            foreach (XmlNode node in personNodes)
            {
                XmlElement xe = (XmlElement)node;//将子节点类型转换为XmlElement类型

                printName = xe.GetAttribute("PrintName");
                number = xe.GetAttribute("Number");//则修改打印次数
                type = xe.GetAttribute("Type");//则修改打印机类型

                DataRow dr = dt.NewRow();
                dr["名称"] = printName;
                dr["次数"] = number;
                dr["类型"] = type;
                dt.Rows.Add(dr);
            }
            return dt;
        }

        private void cTextBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar) && e.KeyChar != '.')
            {
                e.Handled = true;
            }
        }

        private void cTextBox1_TextChanged(object sender, EventArgs e)
        {
            string value = this.cTextBox1.Text;
            int number = 0;
            bool status = false;
            if (value != "")
            {
                try
                {
                    number = int.Parse(value);
                }
                catch { status = true; }

                if (status)
                {
                    Message.MsgBoxInfo("输入必须为整数");
                    this.cTextBox1.Text = "";
                }
                else
                {
                    if (number > 5)
                    {
                        Message.MsgBoxInfo("建议输入次数不能大于5次");
                        this.cTextBox1.Text = "";
                    }
                }
            }
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                basicBLL = new BasicBLL();
                int rowIndex = e.RowIndex;

                string name = dataGridView1.Rows[rowIndex].Cells[0].Value.ToString();
                DeleteXml(name);
                this.dataGridView1.DataSource = ChanageData();

            }
            catch { }
        }
        #region XML 相关操作
   
        /// <summary>
        /// 保存(修改) 打印机信息
        /// </summary>
        /// <param name="printName">打印机名称</param>
        /// <param name="num">次数</param>
        /// <param name="type">类型</param>
        private void SetPrinters(string printName, int num, int type)
        {
           
            bool isExist = false;
            XmlDocument doc = new XmlDocument();
            string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + @"\config.xml";
            doc.Load(path);    //加载Xml文件  
            XmlElement rootElem = doc.DocumentElement;   //获取根节点  

            XmlNodeList personNodes = rootElem.GetElementsByTagName("PrintInfo"); //获取PrintInfo 子节点集合  
            foreach (XmlNode node in personNodes)
            {
                XmlElement xe = (XmlElement)node;//将子节点类型转换为XmlElement类型
                if (xe.GetAttribute("PrintName") == printName)//如果 找到该打印机名称
                {
                    xe.SetAttribute("Number", num.ToString());//则修改打印次数
                    xe.SetAttribute("Type", type.ToString());//则修改打印机类型
                    isExist = true;
                }
            }

            if (!isExist)
            {
                CreatXml(printName, num, type, doc);
            }
            else
            {

                doc.Save("config.xml");
            }
        }
        /// <summary>
        /// 创建XML
        /// </summary>
        /// <param name="printName"></param>
        /// <param name="num"></param>
        /// <param name="type"></param>
        /// <param name="xmlDoc"></param>
        public void CreatXml(string printName, int num, int type, XmlDocument xmlDoc)
        {
            XmlElement rootElem = xmlDoc.DocumentElement;   //获取根节点  
            XmlElement xe1 = xmlDoc.CreateElement("PrintInfo");//创建一个<Node>节点
            xe1.SetAttribute("PrintName", printName);//设置打印机名称
            xe1.SetAttribute("Number", num.ToString());//设置打印次数
            xe1.SetAttribute("Type", type.ToString());//设置打印类型

            rootElem.AppendChild(xe1);
            xmlDoc.AppendChild(rootElem);

            xmlDoc.Save("config.xml");
        }


        public void DeleteXml(string printName)
        {
            XmlDocument doc = new XmlDocument();
            string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + @"\config.xml";
            doc.Load(path);    //加载Xml文件  
            XmlElement rootElem = doc.DocumentElement;   //获取根节点  

            XmlNodeList personNodes = rootElem.GetElementsByTagName("PrintInfo"); //获取PrintInfo 子节点集合  
            foreach (XmlNode node in personNodes)
            {
                XmlElement xe = (XmlElement)node;//将子节点类型转换为XmlElement类型
                if (xe.GetAttribute("PrintName") == printName)//如果 找到该打印机名称
                {
                    rootElem.RemoveChild(xe);
                    break;
                }
            }
            doc.Save("config.xml");


        }
        #endregion

        private void gbtnSet_Click(object sender, EventArgs e)
        {
            string portName = this.cCombox1.Text;

            SetPrinterPort(portName);

            MessageBox.Show("已设置！");
        }

        /// <summary>
        /// 设置打印机端口
        /// </summary>
        /// <param name="portName">端口名称</param>
        private void SetPrinterPort(string portName)
        {
            XmlDocument doc = new XmlDocument();
            string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + @"\config.xml";
            doc.Load(path);    //加载Xml文件  
            XmlElement rootElem = doc.DocumentElement;   //获取根节点  

            XmlNodeList personNodes = rootElem.GetElementsByTagName("PrintSet"); //获取PrintSet 子节点集合  
            foreach (XmlNode node in personNodes)
            {
                XmlElement xe = (XmlElement)node;//将子节点类型转换为XmlElement类型
                xe.SetAttribute("PortName", portName);//则修改打印次数
            }
            doc.Save("config.xml");
        }



        private void ReadXml()
        {

            XmlDocument doc = new XmlDocument();
            string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + @"\config.xml";
            doc.Load(path);    //加载Xml文件  
            XmlElement rootElem = doc.DocumentElement;   //获取根节点  

            XmlNodeList personNodes = rootElem.GetElementsByTagName("PrintSet");
            string portName = "";


            foreach (XmlNode node in personNodes)
            {
                XmlElement xe = (XmlElement)node;//将子节点类型转换为XmlElement类型
                portName = xe.GetAttribute("PortName");
            }

            this.cCombox1.Text = portName;

        }



    }
}
